﻿using jinmaMesProxy.DataAccess;
using jinmaMesProxy.Excep;
using jinmaMesProxy.Model;
using System.Reflection;
using System.Text;

namespace jinmaMesProxy.Service
{
    public class RdRecord08Service
    {
        ErpDbInstance dbInstance;
        ErpVouchQuery<RdRecord08,RdRecords08> query = null;
        public RdRecord08Service()
        {
            dbInstance = ServiceLoader.GetService<ErpDbInstance>();
            ps = typeof(RdRecord08).GetProperties();
            query = new ErpVouchQuery<RdRecord08, RdRecords08>();
        }
        PropertyInfo[] ps;

        public bool Exists(string billCode)
        {
            return query.Exists(billCode);
        }
        public RdRecord08 GetByCode(string code)
        {
            var result = query.GetByCode(code);
            if (result == null) throw new MyException($"找不到名为{code}的其它入库单");
            return result;
        }
        private void InsertAccount(int f, int c)
        {
            string sqlstr = "insert into IA_ST_UnAccountVouch08(IDUN,IDSUN,CVOUTYPEUN,CBUSTYPEUN) VALUES(@p1,@p2,'08','其他入库')";
            using (var cmd = dbInstance.MakeQueryCommand(sqlstr, new KeyValuePair<string, object?>("@p1", f), new KeyValuePair<string, object?>("@p2", c)))
            {
                cmd.ExecuteNonQuery();
            }
        }
        public void Save(RdRecord08 data,bool approved)
        {
            MaterialService materialService = new MaterialService();
            StockService stockService = new StockService();
            var updater = new ErpDbUpdater<RdRecord08, RdRecords08>();
            var (id, did) = SimpleSqlHelper.GetId("rd");
            id++;
            data.Id = 1000000000 + id;
            int i = 1;
            if (String.IsNullOrWhiteSpace(data.BillCode)) throw new MyException("必须指定单号");
            if (String.IsNullOrEmpty(data.Warehouse)) throw new MyException("必须指定仓库");
            SimpleSqlHelper.CheckWarehouse(data.Warehouse);
            if (this.Exists(data.BillCode)) throw new MyException("单号已经存在");
            if (data.Details.Count == 0) throw new MyException("不允许没有明细行的单据");
            if (approved)
            {
                data.Approver = RuntimeVariable.OptUser;
                data.ApproveTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                data.ApproveDate = DateTime.Now.ToString("yyyy-MM-dd 00:00:00.000");
            }
            else
            {
                data.Approver = null;
                data.ApproveTime = null;
                data.ApproveDate = null;
            }
            foreach (var item in data.Details)
            {
                did++;
                item.AutoId = 1000000000 + did;
                item.RowNo = i++;
                if (item.Quantity <= 0) throw new MyException($"物料{item.Material}数量必须大于0");
                var material = materialService.GetByCode(item.Material);
                materialService.CheckFree(material, item);
                materialService.CheckFreeValue(item);
                InsertAccount(data.Id, item.AutoId);
                string[] vfree = [
                   item.Free1==null?"":item.Free1.Trim(),
                   item.Free2==null?"":item.Free2.Trim(),
                   item.Free3==null?"":item.Free3.Trim(),
                   item.Free4==null?"":item.Free4.Trim(),
                   item.Free5==null?"":item.Free5.Trim(),
                   item.Free6==null?"":item.Free6.Trim(),
                   item.Free7==null?"":item.Free7.Trim(),
                   item.Free8==null?"":item.Free8.Trim(),
                   item.Free9==null?"":item.Free9.Trim(),
                   item.Free10==null?"":item.Free10.Trim(),
                ];
                stockService.SetCurrentStock(item.Material, vfree, data.Warehouse,item.Quantity, approved);
            }
            data.TempId = SimpleSqlHelper.GetTempId("0301");
            updater.Insert(data);
            SimpleSqlHelper.WriteId("rd", id, did);
            Logger.Log($"单据{data.BillCode}已被创建");
        }
    }
}
